package com.onec.service.route.controller;

import com.onec.service.route.dao.BlockInfoRepository;
import com.onec.service.route.dao.fluxe.BlockInfoFluxRepository;
import com.onec.service.route.entity.BlockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

public class WebFluxTestController {

    @Autowired
    WebClient webClient;

    @Autowired
    BlockInfoRepository blockInfoRepository;
    @Autowired
    BlockInfoFluxRepository blockInfoFluxRepository;

    @GetMapping
    public Flux<BlockInfo> getBackInfo() {
        return Flux.fromIterable(blockInfoRepository.findAll());
    }

    @GetMapping
    public Mono<BlockInfo> getFluxBackInfo() {
        return blockInfoFluxRepository.findById(1111L);
    }

    /**
     * webclient 的测试数据：
     *
     * @return
     */
    @GetMapping
    public Mono<BlockInfo> webClient() {

        Mono<BlockInfo> blockInfoMono = WebClient.create().
                get()
                .uri("https://test/{}", 100)
                .retrieve()
                .bodyToMono(BlockInfo.class);
        // 处理业务：
        blockInfoMono.subscribe(BlockInfo::getId);

        // 设置超时时间 如果超时就处理e
        blockInfoMono.timeout(Duration.ofSeconds(12)).subscribe(i -> i.getMessage(), e -> e.printStackTrace());

        return blockInfoFluxRepository.findById(1111L);
    }
}
